﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.Specialized;
using System.Reflection;
using Entity;

namespace Web.Helper
{
    public static class PagerHelper
    {
        public static MvcHtmlString AjaxPager<T>(this HtmlHelper html, PagedList<T> pagedList, string UpdateTargetId)
        {
            UIHelper ui = new UIHelper(html.ViewContext.RequestContext.HttpContext.Request.Url.ToString(), UpdateTargetId, pagedList.AddParameters);
            return MvcHtmlString.Create(ui.GetPage(pagedList.PageIndex, pagedList.PageSize, pagedList.TotalCount));
        }

        public static MvcHtmlString AjaxPager<T>(this HtmlHelper html, PagedList<T> pagedList, string UpdateTargetId, string ActionName, string ControllerName)
        {
            string localUrl = string.Format(@"{0}://{1}",
            html.ViewContext.RequestContext.HttpContext.Request.Url.Scheme,
            html.ViewContext.RequestContext.HttpContext.Request.Url.Authority);
            string url = string.Format("{0}/{1}/{2}{3}", localUrl, ControllerName, ActionName, html.ViewContext.RequestContext.HttpContext.Request.Url.Query);

            UIHelper ui = new UIHelper(url, UpdateTargetId, pagedList.AddParameters);
            return MvcHtmlString.Create(ui.GetPage(pagedList.PageIndex, pagedList.PageSize, pagedList.TotalCount));
        }

        public static MvcHtmlString Pager<T>(this HtmlHelper html, PagedList<T> pagedList)
        {
            UIHelper ui = new UIHelper(html.ViewContext.RequestContext.HttpContext.Request.Url.ToString(), pagedList.AddParameters);
            return MvcHtmlString.Create(ui.GetPage(pagedList.PageIndex, pagedList.PageSize, pagedList.TotalCount));
        }

        public static MvcHtmlString TestV(this HtmlHelper htmlHelper, string modelName)
        {
            var metadata = ModelMetadata.FromStringExpression(modelName, htmlHelper.ViewContext.ViewData);

            string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(modelName);
            FieldValidationMetadata validationMetadataForField = htmlHelper.ViewContext.FormContext.GetValidationMetadataForField(modelName, true);

            TagBuilder tag = new TagBuilder("span");
            tag.AddCssClass(HtmlHelper.ValidationMessageCssClassName);
            tag.InnerHtml = fullHtmlFieldName;
            tag.GenerateId("dddddddddd");

            return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));
        }


    }
}
